package com.an.blog.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.an.blog.admin.domain.BlogDailyImage;
import com.an.blog.admin.domain.bo.BlogDailyImageBo;
import com.an.blog.admin.domain.vo.BlogDailyImageVo;
import com.an.blog.mapper.BlogDailyImageMapper;
import com.an.blog.service.IBlogDailyImageService;
import com.an.blog.utils.ConvertUtil;
import com.an.common.constant.BlogConstants;
import com.an.common.core.domain.PageQuery;
import com.an.common.core.page.TableDataInfo;
import com.an.common.utils.StringUtils;
import com.an.system.domain.vo.SysOssVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 博客每日图片Service业务层处理
 *
 * @author Ann
 * @date 2023-09-25
 */
@RequiredArgsConstructor
@Service
public class BlogDailyImageServiceImpl implements IBlogDailyImageService {

    private final BlogDailyImageMapper baseMapper;

    /**
     * 查询博客每日图片
     */
    @Override
    public BlogDailyImageVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询博客每日图片列表
     */
    @Override
    public TableDataInfo<BlogDailyImageVo> queryPageList(BlogDailyImageBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BlogDailyImage> lqw = buildQueryWrapper(bo);
        Page<BlogDailyImageVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        List<BlogDailyImageVo> records = result.getRecords();
        Map<Long, SysOssVo> sysOssVoMap = ConvertUtil.convert2OssIdSysOssMap(records, BlogDailyImageVo::getImage);
        records.forEach(record -> {
            SysOssVo sysOssVo = sysOssVoMap.get(record.getImage());
            record.setImageUrl(sysOssVo != null ? sysOssVo.getUrl() : null);
        });
        return TableDataInfo.build(result);
    }

    /**
     * 查询博客每日图片列表
     */
    @Override
    public List<BlogDailyImageVo> queryList(BlogDailyImageBo bo) {
        LambdaQueryWrapper<BlogDailyImage> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BlogDailyImage> buildQueryWrapper(BlogDailyImageBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BlogDailyImage> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getTitle()), BlogDailyImage::getTitle, bo.getTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), BlogDailyImage::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增博客每日图片
     */
    @Override
    public Boolean insertByBo(BlogDailyImageBo bo) {
        List<String> images = StringUtils.splitList(bo.getImage(), ",");
        if (images.size() > 1) {
            List<BlogDailyImage> blogPhotos = images.stream().map(image -> {
                BlogDailyImage blogDailyImage = new BlogDailyImage();
                blogDailyImage.setTitle(bo.getTitle());
                blogDailyImage.setImage(Long.valueOf(image));
                blogDailyImage.setStatus(bo.getStatus());
                return blogDailyImage;
            }).collect(Collectors.toList());
            return baseMapper.insertBatch(blogPhotos);
        }
        BlogDailyImage dailyImage = BeanUtil.toBean(bo, BlogDailyImage.class);
        return baseMapper.insert(dailyImage) > 0;
    }

    /**
     * 修改博客每日图片
     */
    @Override
    public Boolean updateByBo(BlogDailyImageBo bo) {
        BlogDailyImage update = BeanUtil.toBean(bo, BlogDailyImage.class);
        return baseMapper.updateById(update) > 0;
    }


    /**
     * 批量删除博客每日图片
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 更新每日图像状态
     *
     * @param bo 薄
     * @return int
     * @author: Ann
     * @createDate: 2023/09/25 10:40:53
     * @version: 1.0.0
     */
    @Override
    public int updateDailyImageStatus(BlogDailyImageBo bo) {
        BlogDailyImage blogDailyImage = BeanUtil.toBean(bo, BlogDailyImage.class);
        return baseMapper.updateById(blogDailyImage);
    }

    /**
     * 随机查询列表
     *
     * @param defaultNumber 违约数量
     * @return {@link List }<{@link BlogDailyImageVo }>
     * @author: Ann
     * @createDate: 2023/09/25 11:43:03
     * @version: 1.0.0
     */
    @Override
    public List<BlogDailyImageVo> queryListByRandom(String defaultNumber) {
        return baseMapper.selectVoList(new QueryWrapper<BlogDailyImage>()
                .eq("status", BlogConstants.NORMAL)
            .orderByAsc("rand()").last("LIMIT " + defaultNumber));
    }

}
